/**
 * This file is part of "Subversion Server Configuration (win32)".

    "Subversion Server Configuration (win32)" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Subversion Server Configuration (win32)" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Subversion Server Configuration (win32)".  If not, see <http://www.gnu.org/licenses/>.
 * 
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Subversion_Configuration_Win32
{
    public partial class FORM_Group : Form
    {
        public bool Saved = false;

        private String groupname;
        private String[] users;

        public String GroupName
        {
            get { return groupname; }
        }

        
        public String[] GroupUsers
        {
            get { return users; }
        }


        public FORM_Group(String[] all_users)
        {
            InitializeComponent();
            loadCurrentSvnUsers(all_users);
            this.Text = "New Group";
        }

        public FORM_Group(String[] all_users, String groupName, TreeNodeCollection already_checked_users)
        {
            InitializeComponent();
            loadCurrentSvnUsers(all_users);
            checkUsers(already_checked_users);
            this.textBox_group_name.Text = groupName;
            this.textBox_group_name.ReadOnly = true;
            this.Text = "Modify Group";
        }

        private void checkUsers(TreeNodeCollection already_checked_users)
        {
            if (already_checked_users.Count > 0)
            {
                foreach (TreeNode userNode in already_checked_users)
                {
                    if (!userNode.Text.Equals(""))
                    {
                        int index = this.checkedListBox_users.Items.IndexOf(userNode.Text);
                        this.checkedListBox_users.SetItemChecked(index, true);
                    }
                }
            }
        }

        private void loadCurrentSvnUsers(String[] all_users)
        {
            this.checkedListBox_users.Items.Clear();

            if (all_users.Length > 0)
            {
                foreach (String user in all_users)
                {
                    this.checkedListBox_users.Items.Add(user);
                }
            }
            else throw new Exception("No SVN users");
        }

        private void button_close_cancel_group_Click(object sender, EventArgs e)
        {              
            this.Close();
            this.Saved = false;
        }

        private void button_save_new_group_Click(object sender, EventArgs e)
        {
            if (this.textBox_group_name.Text.Equals(""))
            {
                MessageBox.Show("Please insert a group name", "Missing name");
                return;
            }

            groupname = this.textBox_group_name.Text;

            CheckedListBox.CheckedItemCollection checkedUsers= this.checkedListBox_users.CheckedItems;

            if (checkedUsers.Count < 1)
            {
                MessageBox.Show("Please select at least one user for this group", "No users");
                return;
            }

            users = new String[checkedUsers.Count];
            int i=0;

            foreach (String user in checkedUsers)
            {
                users[i++] = user ;
            }
            
            this.Close();
            this.Saved = true;
        }
        
    }
}